package admin;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * <p>Title: www.AdminPanel </p>
 *
 * <p>Description: Panneau d'administration</p>
 *
 * <p>Copyright: (c) 2008 FSSIM</p>
 *
 * <p>Company: FSSImock</p>
 *
 * @author Antoine Farnault
 * @author Aubry Pirian
 * @version 2.0
 */
public class AdminPanel extends JPanel {
    
    // Variables declaration
    private JScrollPane jScrollPane1;
    private JLabel errorLabel;			// message d'erreur
    private JTextField login;			// case de l'identifiant
    private JPasswordField passwd;		// case du mot de passe
    private JComboBox tablesComboBox;	// Liste de tables
    private JTable tableData;
    private String selectedItem = null;	// Table sélectionnée	
    private JLabel voirTableLabel;
    private tablesComboboxModel tablesComboBoxModel = new tablesComboboxModel(); // modele pour tablesComboBox
    private MemberModel memberModel = new MemberModel();		// modele pour Member
    private RoomModel roomModel = new RoomModel();  			// modele pour Room
    private TP_ThemeModel tp_ThemeModel = new TP_ThemeModel();	// modele pour TP_Theme
    private TP_CardModel tp_CardModel = new TP_CardModel();		// modele pour TP_Card
    // End of variables declaration
    
    private static final String userName = "root";
    private static final String userPasswd = "Password";
    
	private static final long serialVersionUID = 1L;
	
	/**
	 * Creates a new form adminPanel
	 */
    public AdminPanel() {
        initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     */
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        loadLogin();
    }
    
    /**
     * Vérifie les valeurs données dans les champs d'authentification
     * @return true pour autoriser l'accès, false sinon
     */
    private boolean checkValues() {
    	if (! this.login.getText().equalsIgnoreCase(userName)) return false;
    	//FIXME : JPasswordField.getText() est dépréciée, trouver la remplaçante...
    	if (! this.passwd.getText().equals(userPasswd)) return false;
    	return true;
    }
    
    /**
     * Charge le panneau d'authentification
     */
    private void loadLogin() {
    	//FIXME : trouver comment mieux positionner
        FlowLayout fl = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(fl);
        JLabel lab;
        lab = new JLabel("Authentification requise :");
        this.add(lab);
        this.login = new JTextField(15);
        this.add(this.login);
        this.passwd = new JPasswordField(10);
        this.add(this.passwd);
        JButton jb = new JButton("Envoyer");
        jb.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		//System.out.println(e);
        		if (checkValues()) {
        			removeAll();
        			loadAdmin();
        		} else {
        			errorLabel.setText("Identifiants incorrects");
        		}
        	}
        });
        this.add(jb);
        this.errorLabel = new JLabel("");
        this.add(this.errorLabel);
    }
    
    /**
     * Charge le panneau d'administration
     */
    private void loadAdmin() {
        tableData = new javax.swing.JTable();
        voirTableLabel = new javax.swing.JLabel();
        tablesComboBox = new javax.swing.JComboBox();

        tableData.setModel(tp_ThemeModel);
        jScrollPane1.setViewportView(tableData);

        voirTableLabel.setText("Editer la table : ");
        
        tablesComboBox.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		tablesComboBoxActionPerformed();
        	}
        });
        
        tablesComboBox.setModel(tablesComboBoxModel); // on montre cette table par defaut.
        tablesComboBox.setSelectedIndex(0);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(voirTableLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tablesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(465, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(voirTableLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
                    .addComponent(tablesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE))
        );
        tableData.setEnabled(true);
        
		memberModel.setJTable(this.tableData);
		roomModel.setJTable(this.tableData);
		tp_ThemeModel.setJTable(this.tableData);
        tp_CardModel.setJTable(this.tableData);
		
    }
    
    /**
     * Action a la selection d'une table dans la combobox
     */
	private void  tablesComboBoxActionPerformed() {
		selectedItem = tablesComboBox.getSelectedItem().toString();
//		System.out.println(selectedItem);
		if (selectedItem.equalsIgnoreCase("Member")) {
			tableData.setModel(memberModel);
			return;
		}
		if (selectedItem.equalsIgnoreCase("Room")) {
			tableData.setModel(roomModel);
			return;
		}
		if (selectedItem.equalsIgnoreCase("TP_Theme")) {
			tableData.setModel(tp_ThemeModel);
			return;
		}
		if (selectedItem.equalsIgnoreCase("TP_Card")) {
			tableData.setModel(tp_CardModel);
			return;
		}
	}

}
